@(username: String)

$(function() {
    var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
    var chatSocket = new WS("@routes.Application.socket(username).webSocketURL(request)")
    var waitingForActionResponse = false;
    var waitingForCardResponse = false;
	var possibleActions;

    var sendMessage = function() {
    	
    	// Prepare response message
    	var response = {}
		var command = $("#command").val()
    	if (waitingForActionResponse) {
			
			if( command.indexOf(' ') > 0 ){
				command = command.substr(0, command.indexOf(' ')); 
			}
			
			command = "ACTION_" + command.toUpperCase();
			
			if( jQuery.inArray( command, possibleActions) < 0){
				$('#console').append('<label style="color:red">[ERROR] This action is not possible.</label></br>').scrollTop(9999999)
			}
			else{
				response.REQUEST = "RESPONSE_CHOSEN_ACTION"
				response.PARAM_ACTION_STRING = $("#command").val()
			}
    	}
    	else if (waitingForCardResponse) {
    		response.REQUEST = "RESPONSE_CHOSEN_CARDS"
    		response.PARAM_CARDS_STRING = $("#command").val()
    	}
    	else {
    		return;
    	}
    	
        chatSocket.send(JSON.stringify(response))
        $("#command").val('')

    }

    var receiveEvent = function(event) {
        var data = JSON.parse(event.data)
        
        // Keep track of incoming requests
        if (data.REQUEST == "REQUEST_ACTION") {
        	waitingForActionResponse = true;
			possibleActions = data.PARAM_POSSIBLE_ACTIONS;
        }
        else if (data.REQUEST == "REQUEST_CARDS") {
        	waitingForCardResponse = true;
        }
        else {
        	waitingForActionResponse = false;
        	waitingForCardResponse = false;
        }

        // Create the message element     
		if(data.PARAM_MSG != null) {
			var el = $('<label></label><br />')
			$(el).text(data.PARAM_MSG)
			$('#console').append(el).scrollTop(9999999)
		}
        
        var tableSize = $('#plaTab >tbody >tr').length
        
        if(data.PARAM_GAME_STATUS != null){
	        $('#betRou').text(data.PARAM_GAME_STATUS.bettingRound)
	        $('#curBet').text(data.PARAM_GAME_STATUS.betAmount)
	        $('#potSiz').text(data.PARAM_GAME_STATUS.potSize)
	        $('#smaBli').text(data.PARAM_GAME_STATUS.smallBlind)
	        $('#bigBli').text(data.PARAM_GAME_STATUS.bigBlind)
	        $('#numOfP').text(data.PARAM_GAME_STATUS.numberOfPlayers)
	        $('#dealer').text(data.PARAM_GAME_STATUS.playerNames[data.PARAM_GAME_STATUS.playerWithDealerButton])
	       	$('#plaInA').text(data.PARAM_GAME_STATUS.playerNames[data.PARAM_GAME_STATUS.playerInAction])
	       	
	       	if(tableSize != data.PARAM_GAME_STATUS.numberOfPlayers+1){
	       		$('#plaTab tbody').remove('td')
	       		
	       		for(i = 0; i < data.PARAM_GAME_STATUS.numberOfPlayers; i++){
	       			el = $('<tr><td></td><td></td><td></td><td></td></tr>')
	       			
	       			$(el).attr('id','player'+i);
	       			$('#plaTab').append(el)
	       		}
	       	}
	       	
       		for(i = 0; i < data.PARAM_GAME_STATUS.numberOfPlayers; i++){		       			
       			$('#player'+i).find('td').eq(0).text(data.PARAM_GAME_STATUS.playerNames[i])
       			$('#player'+i).find('td').eq(1).text(data.PARAM_GAME_STATUS.playerChipCounts[i])
       			$('#player'+i).find('td').eq(2).text(data.PARAM_GAME_STATUS.playerPotContributions[i])
       			$('#player'+i).find('td').eq(3).text(data.PARAM_GAME_STATUS.playerStatuses[i])
       		}
	       	
	       	if(data.PARAM_GAME_STATUS.myCards != null){
		       	for(i = 0; i < data.PARAM_GAME_STATUS.myCards.length; i++){
		       		$('#card'+i).text(data.PARAM_GAME_STATUS.myCards[i])
		       	}
		    }
	    }   
    }

    var handleReturnKey = function(e) {
        if(e.charCode == 13 || e.keyCode == 13) {
            e.preventDefault()
            sendMessage()
        }
    }

    $("#command").keypress(handleReturnKey)


    chatSocket.onmessage = receiveEvent

})
